﻿#include "IAlgorithmPluginOperator.h"
#include "PluginManager.h"

namespace CVV
{

namespace Interface
{


IAlgorithmPluginOperator::IAlgorithmPluginOperator(QObject* parent)
    : QObject (parent)
{

}

IAlgorithmPluginOperator::~IAlgorithmPluginOperator()
{

}

QString IAlgorithmPluginOperator::operatorName() const
{
    return m_operatorName;
}

void IAlgorithmPluginOperator::setOperatorName(const QString &peratorName)
{
    m_operatorName = peratorName;
}

std::shared_ptr<IPluginParam> IAlgorithmPluginOperator::getAlgorithmPluginResultOutput()
{
    return (std::make_shared<IPluginParam>());
}

std::shared_ptr<IAlgorithmPluginParam> IAlgorithmPluginOperator::getAlgorithmPluginParam(const QString &flowId, const QString &cellId)
{
    if (flowId.isEmpty()
            || cellId.isEmpty())
    {
        return nullptr;
    }

    if (m_hashAlgorithmPluginParam.contains(flowId)
            && m_hashAlgorithmPluginParam[flowId].contains(cellId))
    {
        return m_hashAlgorithmPluginParam.value(flowId).value(cellId);
    }

    return nullptr;
}

std::shared_ptr<IPluginParam> IAlgorithmPluginOperator::getAlgorithmPluginResultOutput(const QString &flowId, const QString &cellId)
{
    if (flowId.isEmpty()
            || cellId.isEmpty())
    {
        return nullptr;
    }

    if (m_hashPluginResultOutput.contains(flowId)
            && m_hashPluginResultOutput[flowId].contains(cellId))
    {
        return m_hashPluginResultOutput.value(flowId).value(cellId);
    }

    return nullptr;
}

std::shared_ptr<IAlgorithmPluginParam> IAlgorithmPluginOperator::algorithmPluginParam(const QString &pluginCode,
                                                                                      const QString &flowId,
                                                                                      const QString &cellId)
{
    if (pluginCode.isEmpty()
            || flowId.isEmpty()
            || cellId.isEmpty())
    {
        return nullptr;
    }

    QString tmpPluginCode = pluginCode;
    if (!tmpPluginCode.endsWith("Algorithm"))
    {
        tmpPluginCode += "Algorithm";
    }

    IAlgorithmPluginOperator* opt = dynamic_cast<IAlgorithmPluginOperator*>
            (CVV::Infrastructure::ExtensionSystem::PluginManager::getObjectByName(tmpPluginCode));

    if(opt)
    {
        return opt->getAlgorithmPluginParam(flowId, cellId);
    }

    return nullptr;
}

std::shared_ptr<IPluginParam> IAlgorithmPluginOperator::algorithmPluginResultOutput(const QString &pluginCode,
                                                                                    const QString &flowId,
                                                                                    const QString &cellId)
{
    if (pluginCode.isEmpty()
            || flowId.isEmpty()
            || cellId.isEmpty())
    {
        return nullptr;
    }

    IAlgorithmPluginOperator* opt = dynamic_cast<IAlgorithmPluginOperator*>
            (CVV::Infrastructure::ExtensionSystem::PluginManager::getObjectByName(pluginCode));

    if(opt)
    {
        return opt->getAlgorithmPluginResultOutput(flowId, cellId);
    }

    return nullptr;
}

void IAlgorithmPluginOperator::removeAlgorithmParam(const QString &flowId, const QString &cellId)
{
    if (m_hashAlgorithmPluginParam.contains(flowId)
            && m_hashAlgorithmPluginParam.value(flowId).contains(cellId))
    {
        m_hashAlgorithmPluginParam[flowId].remove(cellId);
    }
}

void IAlgorithmPluginOperator::removeAlgorithmParam(const QString &flowId)
{
    if (m_hashAlgorithmPluginParam.contains(flowId))
    {
        m_hashAlgorithmPluginParam.remove(flowId);
    }
}

void IAlgorithmPluginOperator::sendSoftwareTriggerCommand(std::shared_ptr<IAlgorithmPluginParam> pResult)
{
    Q_UNUSED(pResult)
}

void IAlgorithmPluginOperator::getCameraParams(std::shared_ptr<IAlgorithmPluginParam> pResult)
{
    Q_UNUSED(pResult)
}

void IAlgorithmPluginOperator::enumCameras(const QString &cameraType, QStringList &lstCamera)
{
    Q_UNUSED(cameraType)
    Q_UNUSED(lstCamera)
}

void IAlgorithmPluginOperator::setCameraTriggerOff()
{

}


}
}
